using System;
using System.Collections.Generic;
using GGEditor.API;
using GGEditor.API.Editors;
using GGEditor.API.Plugins;
using GGEditor.Core.Editors;
using GGEditor.Core.UI;

namespace GGEditor.Core.Documents
{
	public class DocumentManager
	{
		private IList<IDocument> documents = new List<IDocument>();
		private IDocument activeDocument;
		private bool hookedWindowManager = false;

		private DocumentManager()
		{
			
		}

		void OnWindowActivated(IWindowManager wm, IWindow window)
		{
			foreach (IDocument document in Documents)
			{
				if (document.Window == window)
				{
					activeDocument = document;
					break;
				}
			}
		}

		public IEnumerable<IDocument> Documents
		{
			get { return documents; }
		}

		public IDocument ActiveDocument
		{
			get { return activeDocument; }
		}

		protected IWindowManager WindowManager
		{
			get
			{
				return PluginContext.Instance.GetObject<IWindowManager>(
					"/Workspace/Window/Manager");
			}
		}

		public void New(string editorName)
		{
			IEditor editor = EditorManager.Instance.
				CreateEditor(editorName);
			Open(editor);
		}

		public void Open(string filePath)
		{
			IEditorModel model = ImporterManager.Instance.Open(filePath);
			if (model == null)
				throw new ApplicationException();
			IEditor editor = EditorManager.Instance.
				CreateEditor(model);
			Open(editor);
		}

		private void Open(IEditor editor)
		{
			if (!hookedWindowManager)
			{
				HookWindowManager();
				hookedWindowManager = true;
			}

			Document document = new Document()
			{
				Editor = editor
			};
			documents.Add(document);
			IWindow window = WindowManager.CreateWindow(
				new WindowParameters()
				{
					Content = editor.View.Control,
					IsDocument = true,
					Text = editor.Title,
					Visible = false
				});
			document.Window = window;
			document.Window.Visible = true;
		}

		private void HookWindowManager()
		{
			WindowManager.WindowActivated += OnWindowActivated;
		}

		#region XmlNodeObjectFactory

		[XmlNodeObjectFactory(Name = "DocumentManager")]
		public class DocumentManagerFactory : IXmlNodeObjectFactory
		{
			#region Implementation of IXmlNodeObjectFactory

			public object Build(IPluginNode pluginNode, IDictionary<string, string> attributes)
			{
				return Instance;
			}

			#endregion
		}

		#endregion

		#region Singleton

		private static DocumentManager instance = new DocumentManager();

		public static DocumentManager Instance
		{
			get { return instance; }
		}

		#endregion
	}

	public interface IDocument
	{
		string FilePath { get; }
		string FileName { get; }
		IEditor Editor { get; }
		IWindow Window { get; }

		void Save();
		void SaveAs(string filePath);
	}

	class Document : IDocument
	{
		#region Implementation of IDocument

		public string FilePath { get; set; }
		public string FileName { get; set; }
		public IEditor Editor { get; set; }
		public IWindow Window { get; set; }

		public void Save()
		{

		}

		public void SaveAs(string filePath)
		{
			ExporterManager.Instance.Save(Editor.Model, filePath);
		}

		#endregion
	}
}